#include <interfacelib/window/loadcontrol.h>
#include "Dialog/MakeRoomDialog.h"
#include "Define/control.h"
#include <interfacelib/window/button.h>
#include "Network/NetworkManager.h"
#include <netlib/packet/packetpool.h>
#include "../../common/protocol.h"


MakeRoomDialog::MakeRoomDialog() : m_editRoomName( NULL )
{
	ADD_USERFUNCTION( "OnClickMakeRoomOK", onMakeRoomOKClick );
	ADD_USERFUNCTION( "OnClickMakeRoomCancel", onMakeRoomCancelClick );
}

MakeRoomDialog::~MakeRoomDialog()
{
}

bool MakeRoomDialog::create( interfacelib::Window::LoadControl *loadControl )
{
	interfacelib::Window::LoadControl::ControlProperty *property = loadControl->getControlProperty( SCRIPT_MAKE_ROOM_DIALOG_NAME );
	if ( property )
		Panel::create( property->resourceName, property->xpos, property->ypos , property->attribute );

	loadControl->setLinkFunctionPanel( this );
	loadControl->loadChild( SCRIPT_MAKE_ROOM_DIALOG_NAME, this );

	m_editRoomName = ( interfacelib::Window::EditBox * )loadControl->getControl( "RoomName", interfacelib::Window::Window::WindowType::EDITBOX );
	return ( true );
}

void MakeRoomDialog::onMakeRoomOKClick()
{
	SYNC_GET_PACKET( syncGetPacket, makeRoomPacket );
	makeMakeRoomPacketReq( makeRoomPacket, ( char *)m_editRoomName->getBuffer() );

	NetworkManager::getInstance()->send( makeRoomPacket );
}

void MakeRoomDialog::onMakeRoomCancelClick()
{
	setVisible( false );
}

bool MakeRoomDialog::makeMakeRoomPacketReq( netlib::packet::Packet *packet, char *roomName )
{
	if ( !roomName ) return ( false );
	packet->ID( protocol::community::CreateChannelReq );
	*packet << roomName;

	return ( true );
}